package com.cy;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.TimeUnit;

@RestController
public class DegradeConsumerController{

    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    private RestTemplate loadBalancedRestTemplate;

    @GetMapping("/consumer/doRestEcho4")
    public String doLoadBalanceRestTemplateEcho(){

        try {
            //休眠的两种方式，模拟慢调用
            //Thread.sleep(200);
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {}

        String url=String.format("http://nacos-provider/provider/echo/%s",appName);
        //向服务提供方发起http请求,获取响应数据
        return loadBalancedRestTemplate.getForObject(
                url,//要请求的服务的地址
                String.class);//String.class为请求服务的响应结果类型(responseType)
    }

}
